import request from '@/utils/request.js'

export async function login(payload) {
  // 后台管理员登录：POST /admin/login，入参 { aname, password }
  const body = {
    aname: payload.username ?? payload.aname,
    password: payload.password,
  }
  const res = await request.post('/admin/login', body)
  // 成功后保存 Sa-Token
  if (res && res.code === '200' && res.data?.tokenValue) {
    localStorage.setItem('token', res.data.tokenValue)
    localStorage.setItem('tokenName', res.data.tokenName || 'Authorization')
  }
  return res
}

export async function logout() {
  // 管理员注销：POST /admin/logout
  try {
    const res = await request.post('/admin/logout')
    return res
  } finally {
    localStorage.removeItem('token')
    localStorage.removeItem('tokenName')
  }
}